package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.constant.UserConstant;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.BatteryModel;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.value.QueryVO;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.base.BatteryModelService;
import com.tbit.utils.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * 电池方案
 *
 * @author Leon
 * 2017年2月24日 下午5:51:05
 */
@Validated
@RestController
@RequestMapping(value = "/batteryModel", produces = BaseController.MEDIA_JSON_UTF8)
public class BatteryModelController extends BaseController {
    @Autowired
    private BatteryModelService batteryModelService;

    /**
     * 添加电池方案
     *
     * @param batteryModel 电池方案
     * @param request      请求模型
     * @return .
     */
    @RequestMapping("/add")
    @OperateLog(module = ModuleEnum.BATTERYMODEL, methods = MethodEnum.BATTERYMODEL_ADD)
    public JsonResult add(BatteryModel batteryModel, HttpServletRequest request) {

        Assert.notNullAndHasLength(batteryModel.getName(), "ver.name.notBlank");
        checkLength(batteryModel);

        LoginInfo loginInfo = getLogin(request);
        if (loginInfo.type.equals(UserConstant.USER_AGENT) || loginInfo.type.equals(UserConstant.USER_FRANCHISEE)) {
            batteryModel.setType(UserConstant.USER_AGENT);
        } else {
            batteryModel.setType(UserConstant.USER_ADMIN);
        }
        if (batteryModel.getAccountId() == null) {
            batteryModel.setAccountId(loginInfo.id);
        }
        //电池方案类型，电压或者BMS
        if (batteryModel.getBatteryType() == null) {
            batteryModel.setBatteryType(0);
        }
        batteryModel.setAddTime(new Date());
        batteryModel.setCreateBy(loginInfo.getName());
        batteryModelService.insert(batteryModel, loginInfo);

        return JsonResult.ok();
    }

    @RequestMapping("/edit")
    public JsonResult edit(@RequestBody(required = false) QueryVO ob) {
        BatteryModel batteryModel = JSON.toJavaObject(ob.getObject(), BatteryModel.class);
        checkLength(batteryModel);
        batteryModelService.edit(batteryModel);
        return JsonResult.ok();
    }

    /* 删除 */
    @RequestMapping("/del")
    @OperateLog(module = ModuleEnum.BATTERYMODEL, methods = MethodEnum.BATTERYMODEL_DEL)
    public JsonResult del(@NotNull(message = "ver.batteryModelId.notNull") Integer batteryModelId, HttpSession session, HttpServletRequest request) {
        /* 校验 */
        Assert.notNull(batteryModelId, "ver.batteryModelId.notBlank");
        LoginInfo loginInfo = getLogin(request);
        batteryModelService.del(batteryModelId, loginInfo);

        return JsonResult.ok(null);
    }

    /* 更加平台账号id查询 */
    @RequestMapping("/getByAccountId")
    public JsonResult getByAccountId(Integer accountId,
                                     String name,
                                     @RequestParam(defaultValue = "10") Integer rowCount,
                                     @RequestParam(defaultValue = "1") Integer pageNO,
                                     HttpServletRequest request) {
        LoginInfo loginInfo = getLogin(request);
        return batteryModelService.queryPage(accountId, name, rowCount, pageNO, loginInfo);
    }

    /**
     * 获取电池方案模板
     *
     * @param request
     * @return
     */
    @RequestMapping("/getBatteryTemplate")
    public JsonResult getBatteryTemplate(HttpServletRequest request, Integer accountId) {

        List<BatteryModel> batteryModels = batteryModelService.getBatteryTemplate(accountId);

        return JsonResult.ok(batteryModels);
    }

    /* 添加 */
    @RequestMapping("/addList")
    @OperateLog(module = ModuleEnum.BATTERYMODEL, methods = MethodEnum.BATTERYMODEL_ADD)
    public JsonResult addList(Integer accountId, String remark,
                              @NotNull(message = "ver.name.notNull") String name,
                              @NotNull(message = "ver.maxVols.notNull") String maxVols,
                              @NotNull(message = "ver.minVols.notNull") String minVols,
                              @NotNull(message = "ver.batteryPers.notNull") String batteryPers,
                              @NotNull(message = "ver.milages.notNull") String milages,
                              HttpServletRequest request) {
        LoginInfo loginInfo = getLogin(request);
        if (!loginInfo.type.equals(UserConstant.USER_AGENT)) {
            Assert.notNull(accountId, "ver.accountId.notNull");
        } else {
            accountId = loginInfo.id;
        }
        if (remark == null) {
            remark = "";
        }
        batteryModelService.addBatteryTemplate(loginInfo, accountId,
                name, remark, maxVols, minVols, batteryPers, milages);
        return JsonResult.ok();
    }

    void checkLength(BatteryModel batteryModel){
        if (StrUtil.isNotEmpty(batteryModel.getName()) && batteryModel.getName().length() > 100){
            throw new BaseException("电池方案名称过长");
        }else if (StrUtil.isNotEmpty(batteryModel.getRemark()) && batteryModel.getRemark().length() > 255){
            throw new BaseException("电池方案备注过长");
        }
    }
}